package cn.getech.data.development.service.impl;

import cn.getech.data.development.entity.TablePartitionStatistics;
import cn.getech.data.development.mapper.TablePartitionStatisticsMapper;
import cn.getech.data.development.model.vo.TablePartitionStatisticsVO;
import cn.getech.data.development.service.TablePartitionStatisticsService;
import cn.getech.data.intelligence.common.utils.ByteConverFormat;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service
public class TablePartitionStatisticsServiceImpl extends ServiceImpl<TablePartitionStatisticsMapper, TablePartitionStatistics> implements TablePartitionStatisticsService {

    @Resource
    private TablePartitionStatisticsMapper mapper;

    @Override
    public List<TablePartitionStatisticsVO> sizeTopTenRanking(Integer tableId, Integer sort) {
        sort = sort == 1 ? 1 : 2;
        List<TablePartitionStatisticsVO> resultList = mapper.sizeTopTenRanking(tableId, sort);
        if (CollectionUtil.isNotEmpty(resultList)) {
            resultList.stream().forEach(item -> {
                item.setSizeStr(ByteConverFormat.convertFormat(item.getSize()));
            });
        } else {
            resultList = new ArrayList<>();
        }
        return resultList;
    }

    @Override
    public List<TablePartitionStatisticsVO> selectPartitionStoreList(Integer tableId, String unit) {
        List<TablePartitionStatisticsVO> restList= Lists.newArrayList();
        List<TablePartitionStatistics> list = mapper.selectList(
                new QueryWrapper<TablePartitionStatistics>().eq("table_id", tableId));
        for (TablePartitionStatistics tablePartitionStatistics : list) {
            TablePartitionStatisticsVO vo=new TablePartitionStatisticsVO(
                    tablePartitionStatistics.getId(), tableId, tablePartitionStatistics.getPartitionValue(),
                    tablePartitionStatistics.getPartitionValue(),  tablePartitionStatistics.getRowsNum(),
                    tablePartitionStatistics.getSize());
            vo.setFileNum(tablePartitionStatistics.getFileNum());
            Double digit = tablePartitionStatistics.getSize();
            DecimalFormat df = new DecimalFormat("0.000");
            if ("KB".equals(unit)) {
                String value = df.format(digit / 1024);
                vo.setSizeStr(value + unit);
            }else  if ("M".equals(unit)) {
                String value = df.format(digit / (1024 *1024 ));
                vo.setSizeStr(value + unit);
            } else if ("G".equals(unit)) {
                String value = df.format(digit / (1024 * 1024 * 1024));
                vo.setSizeStr(value + unit);
            } else if ("T".equals(unit)) {
                long s = 1024 * 1024 * 1024 * 1024L;
                String value = df.format(digit / s);
                vo.setSizeStr(value + unit);
            }
            restList.add(vo);
        }
        return restList;
    }

    @Override
    public void batchSaveOrUpdate(List<TablePartitionStatistics> entityList) {
        mapper.batchSaveOrUpdate(entityList);
    }
}
